#disk monitor setting
$thresholdValue = 1024*1024*1024*5
$logfileDirs = "C:\temp","d:\bpodb\index","d:\bpodb\status"
$daysKeep = 10

#email setting
$smtpServer = "internalmail.XXXXXX.com"
$smtpUser = "bpotester@XXXXXX.com"
$smtpPassword = "ZZZZZ"
$MailAddress="bpotester@XXXXXX.com"
$MailtoAddress="YYY@XXXXXX.com"

#
$server = gc env:computername
$disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3";
$time = (Get-Date).ToString("yyyy_MM_dd_HH_mm")

$body = @()
$reportFile = "$server"+"_$time.html"
$reportFile

#move log file mudule
function moveLogfile
{	
	$body += "<br><br><h1>Log file maintain report for <u>$server</u></h1><br>"
	
	#check if report file exist
	if(Test-Path $reportFile)
	{
		Remove-Item $reportFile -force
	}
	else
	{
		Out-File $reportFile
	}	
	
	foreach($logfileDir in $logfileDirs)
	{	
		if(!(Test-Path $logfileDir)) 
		{continue;}
		else
		{
			# get file list
			$logFiles = Get-ChildItem -path $logfileDir -Recurse|where-object { -not $_.PSIsContainer -and $_.lastwritetime -lt (get-date).adddays(-$daysKeep)}
			
			foreach ($logFile in $logFiles)
			{	
				if($logFile -eq $Null){continue;}
				else
				{
					Write-Host ">>>>>>>>>>>>>>>>>>>>>>> deleting " $logFile.FullName
			    	Remove-Item -Recurse -force $logFile.FullName
			    	
			    	#report content
					$body += "Deleting " + $logFile.FullName + "<br>"
				}
			}		
		}
	}

	$body | Out-File -Append -Force $reportFile
}

#check disk space module
$freeSpaceArray = @();
function checkSpace
{
	#generate report content
	$body += "<br><br><br><h1>Disk space status report for <u>$server</u></h1><br>"
	$body += "Report generated at : " + $time + "<br><br>"
	$body += "--------------------------------------------------------------------<br>"
		
	foreach($disk in $disks)
	{
		$deviceID = $disk.DeviceID
		[float]$size = $disk.Size
		[float]$freespace = $disk.FreeSpace
	 
		$percentFree = [Math]::Round(($freespace / $size) * 100, 2)
		$sizeGB = [Math]::Round($size / 1073741824, 2)
		$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2)
		
		$freeSpaceArray += $freeSpaceGB
		
		#report content
		$body += "Free disk space(GB) in "+ $deviceID +" "+ $freeSpaceGB + "/" + $sizeGB + ", free precent is : " + $percentFree +" %<br>"
	}
	
	#generate report content
	$body
	$body += "--------------------------------------------------------------------<br>"
	
	#write to file
	$body | Out-File -Append -Force $reportFile
	
	#disk space check
	Foreach ($freeSpaceValue in $freeSpaceArray)
	{
		if($freeSpaceValue -le $thresholdValue)
		{
			moveLogfile		
			sendEmail
			break
		}
	}
}

#send warning email module
function sendEmail
{
	#create the mail message
	$mail = New-Object System.Net.Mail.MailMessage
	
	$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
	$mail.To.Add($MailtoAddress)
	
	#set the content
	$mail.IsBodyHTML = $true
	$mail.Subject = "LOW DISK SPACE IN " + $server
	$mail.Priority  = "High"
	$mail.Body = "Server : " + $server + "<br><br>" + "Please refer to attachment for details."
	
	$directory = Get-Location
	$diskInfo = $reportFile
	$dPath = [IO.Path]::GetFullPath($directory) 
	$attachedfilename=$dPath + "\" + $reportFile
	$attachment = new-Object System.Net.Mail.Attachment($attachedfilename)
	$mail.Attachments.Add($attachment)
	
	#send the message
	$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
	$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
	$smtp.Send($mail)
}

#__main__:
#remove report file
get-childitem * -include *.html | remove-item -force

#check disk space
checkSpace
